package com.broadvision.bvgs.multilang;

import com.broadvision.data.client.Content;
import com.broadvision.data.common.ListUpdater;
import com.broadvision.data.common.RowData;
import com.broadvision.data.common.TableData;
import com.broadvision.data.exceptions.CategoryNotFoundException;
import com.broadvision.data.exceptions.ContentNotFoundException;
import com.broadvision.data.exceptions.DataAlreadyModifiedException;
import com.broadvision.data.exceptions.DataDeadLockedException;
import com.broadvision.data.exceptions.DataException;
import com.broadvision.data.exceptions.DuplicateDataException;
import com.broadvision.web.util.ViewDynaBean;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionMapping;

public class MultilangUtils
{
	public static void removeMultilangName(Content content, String language, String translationTableName, String translationLanguageFieldName){
    	TableData translationTable = getTranslationList(content, translationTableName);
    	ListUpdater listUpdater = new ListUpdater();
    	int currentTranslationIndex = translationTable.findRow(translationLanguageFieldName, language);
       	if (currentTranslationIndex != -1){
       		listUpdater.recordDelete(translationTableName, translationTable.getRow(currentTranslationIndex));
       	   	try {
    			content.modifyListData(listUpdater);
    		} catch (DataDeadLockedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (DuplicateDataException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (DataAlreadyModifiedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (ContentNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (DataException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
       	}
	}
	
    public static void addMultilangName(Content content, String language, String text, String translationTableName, String translationLanguageFieldName, String translationNameFieldName){
    	
    	System.out.println("inside addMultilangName() of class MultilangUtils" );
        
    	System.out.println("***************************" );
        
        System.out.println("translationTableName: "+translationTableName );
        System.out.println("translationLanguageFieldName: "+translationLanguageFieldName );
        System.out.println("translationNameFieldName: "+translationNameFieldName );
        System.out.println("***************************" );
        
    	
        System.out.println("1" );
    	TableData translationTable = getTranslationList(content, translationTableName);
    	 System.out.println("2" );
    	
    	
    	
    	ListUpdater listUpdater = new ListUpdater();
    	System.out.println("3" );
    	RowData newRow = new RowData();
    	System.out.println("4" );
    	newRow.setStringValue(translationLanguageFieldName, language);
    	System.out.println("5" );
    	newRow.setStringValue(translationNameFieldName, text);
    	System.out.println("6" );
    	int currentTranslationIndex = translationTable.findRow(translationLanguageFieldName, language);
    	System.out.println("7" );
    	if (currentTranslationIndex != -1){
    		System.out.println("8" );
    		listUpdater.recordUpdate(translationTableName, translationTable.getRow(currentTranslationIndex), newRow);
    	} else {
    		System.out.println("9" );
    		
    		System.out.println("translationTableName: "+translationTableName );
    		System.out.println("newRow: "+newRow.toString());
    		listUpdater.recordInsert(translationTableName, newRow);
    	}
    	try {
    		System.out.println("10" );
			content.modifyListData(listUpdater);
			System.out.println("11" );
		} catch (DataDeadLockedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DuplicateDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DataAlreadyModifiedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ContentNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("12" );
    }
    
    public static ViewDynaBean prepareMultilangNameBean(ActionMapping mapping, String language, String text)
        throws Exception
    {
        ViewDynaBean bean = new ViewDynaBean(DTO_BEAN_NAME, mapping);
        PropertyUtils.setSimpleProperty(bean, "language", language);
        PropertyUtils.setSimpleProperty(bean, "text", text);
        return bean;
    }
 
    public static TableData getTranslationList(Content content, String translationTable){
		try {
			TableData translationList = content.getListData(translationTable);
			return translationList;
		} catch (DataDeadLockedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CategoryNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  catch (DataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return null;
    }
 
    public static final String DTO_BEAN_NAME = "epIMultilangNameDTO";
}